// ==UserScript==
// @name         百度搜索过滤
// @namespace    https://gitee.com/huelse/js-scripts/raw/master/baidu-filter.js
// @version      2.1.2
// @description  在block_list中添加你想过滤的关键词，世界都将清净了
// @author       THENDINGs
// @require      https://unpkg.com/jquery@3.6.0/dist/jquery.min.js
// @match        https://www.baidu.com/*
// @icon         https://www.baidu.com/favicon.ico
// @grant        unsafeWindow
// @license      GPLv3 License
// ==/UserScript==

(function() {
    'use strict';
    var $ = unsafeWindow.jQuery;

    // 屏蔽关键词列表
    const block_list = ['baijiahao', 'CSDN', '广告'];
    // 屏蔽模式
    const mode = 0;

    function block() {
        if (mode === 1) {
            block_all();
        } else {
            block_span();
        }
    }

    function block_all() {
        const results = $('#content_left > div');
        for (let i = 0; i < results.length; i++) {
            const result = $(results[i]);
            // 删除块广告
            if (!result.attr('mu')) {
                // 避免卡片结果被删除
                if (!result.prop('className').includes('c-group-wrapper')) {
                    result.remove();
                }
            } else {
                // 删除关键词
                for (let j = 0; j < block_list.length; j++) {
                    if (results[i].innerText.includes(block_list[j])) {
                        result.remove();
                        break;
                    }
                }
            }
        }
    }

    function block_span() {
        const span_results = $('#content_left > div span,#content_left > div a');
        for (let i = 0; i < span_results.length; i++) {
            const span = $(span_results[i]);
            for (let j = 0; j < block_list.length; j++) {
                if (span_results[i].innerText.includes(block_list[j])) {
                    let parents = span.parents('.c-container');
                    if (parents.length > 0) {
                        parents[0].remove();
                        break;
                    }
                }
            }
        }
    }

    function relink() {
        const as = $('#content_left a');
        for (let i = 0; i < as.length; i++) {
            const cl = as[i].className.replace(/[\r\n\ ]/g, '');
            if (!cl || cl.length < 1) {
                let url = as[i].href.replace('http://', 'https://');
                if (!url.includes('eqid=')) url += '&wd=&eqid=';
                if (/\:\/\/www.baidu.com(?!\/s)/.test(url)) {
                    $.get(url, function(data) {
                        const real_url = /URL='(.+)'">/.exec(data);
                        if (real_url && real_url[1]) {
                            if (real_url[1].includes('baijiahao')) {
                                $(as[i]).parents('.result').remove();
                            } else {
                                as[i].href = real_url[1];
                            }
                        }
                    });
                }
            }
        }
    }

    document.querySelector('#wrapper_wrapper').addEventListener("DOMNodeInserted", function (e) {
        if (e.target.id == 'container') {
            setTimeout(block(), 100);
            setTimeout(relink(), 200);
        }
        if (e.target.id == '1') {
            setTimeout(block(), 100);
        }
    });

    $(function() {
        setTimeout(block(), 100);
        setTimeout(relink(), 200);
    });

})();
